package fr.ul.ade.secunde.ui;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.SherlockFragment;

import fr.ul.ade.R;
import fr.ul.ade.secunde.database.AdeWebContract;
import fr.ul.ade.secunde.util.Bundles;
import fr.ul.ade.secunde.util.UiUtils;

/**
 * Fragment affichant le détails d'un emploi du temps donné.
 * 
 * @author Michaël Schnell
 * @since Jul 2, 2013
 * 
 * @version 1.6
 */
public class ScheduleDetailsFragment
		extends SherlockFragment
		implements LoaderCallbacks<Cursor> {

	public static ScheduleDetailsFragment newInstance(long scheduleId, String scheduleName) {
		ScheduleDetailsFragment fragment = new ScheduleDetailsFragment();
		Bundle args = new Bundle();
		args.putLong(Bundles.SCHEDULE_DETAILS_ID, scheduleId);
		args.putString(Bundles.SCHEDULE_DETAILS_NAME, scheduleName);
		fragment.setArguments(args);
		return fragment;
	}

	// --------------------------------------------------------
	//
	// Création
	//
	// --------------------------------------------------------

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_schedule_details,
				container,
				false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
	}

	@Override
	public void onResume() {
		super.onResume();

		final Loader<Cursor> loader = this.getLoaderManager()
				.getLoader(ScheduleQuery._TOKEN);
		if (loader != null) {
			// IMP Recharcher les données que s'il y a un changement
			loader.forceLoad();
		} else {
			this.getLoaderManager().initLoader(ScheduleQuery._TOKEN,
					this.getArguments(),
					this);
		}
	}

	/**
	 * Remplie le contenu de ce fragment avec les valeurs du {@link Cursor}
	 * fourni.
	 * 
	 * @param cursor
	 *            {@link Cursor} contenant les valeurs à afficher
	 */
	private void fillView(final Cursor cursor) {
		final View view = this.getView();

		UiUtils.setTextViewContent(view,
				R.id.schedule_details_description,
				cursor.getString(ScheduleQuery.SCHEDULE_DESCRIPTION));
		String lastUpdate = cursor
				.getString(ScheduleQuery.SCHEDULE_LAST_UPDATE);

		if (TextUtils.isEmpty(lastUpdate)) {
			lastUpdate = this.getActivity().getResources()
					.getString(R.string.schedule_details_last_update_never);
		}
		UiUtils.setTextViewContent(view,
				R.id.schedule_details_last_update,
				lastUpdate);

		UiUtils.setTextViewContent(view,
				R.id.schedule_details_resources,
				cursor.getString(ScheduleQuery.SCHEDULE_RESOURCES));
		UiUtils.setTextViewContent(view,
				R.id.schedule_details_student_numbers,
				cursor.getString(ScheduleQuery.SCHEDULE_STUDENT_NUM));

		UiUtils.setTextViewContent(view,
				R.id.schedule_details_period,
				this.getResources()
						.getString(R.string.schedule_details_period_text,
								cursor.getString(ScheduleQuery.SCHEDULE_PERIOD)));

		UiUtils.setTextViewContent(view,
				R.id.schedule_details_project_name,
				cursor.getString(ScheduleQuery.SCHEDULE_PROJECT_NAME));

		UiUtils.setTextViewContent(view,
				R.id.schedule_details_created,
				cursor.getString(ScheduleQuery.SCHEDULE_CREATED));
	}

	// --------------------------------------------------------
	//
	// Traitement des données
	//
	// --------------------------------------------------------

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		Uri scheduleUri = AdeWebContract.Schedule.buildUri(args
				.getLong(Bundles.SCHEDULE_DETAILS_ID));
		String[] projection = ScheduleQuery.PROJECTION;

		return new CursorLoader(this.getActivity(), scheduleUri, projection,
				null, null, AdeWebContract.Schedule.DEFAULT_SORT);
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {
		/* Nothing to be done. */
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		if (this.getActivity() == null) {
			return;
		}// if

		if (data.moveToFirst()) {
			this.fillView(data);
		}
	}

	// --------------------------------------------------------
	//
	// Données chargées
	//
	// --------------------------------------------------------

	protected interface ScheduleQuery {
		final int _TOKEN = 0x1;

		final String[] PROJECTION = { BaseColumns._ID,
				AdeWebContract.Schedule.SCHEDULE_NAME,
				AdeWebContract.Schedule.SCHEDULE_DESCRIPTION,
				AdeWebContract.Schedule.SCHEDULE_RESOURCES,
				AdeWebContract.Schedule.SCHEDULE_STUDENT_NUM,
				AdeWebContract.Schedule.SCHEDULE_PERIOD,
				AdeWebContract.Schedule.SCHEDULE_PROJECT_NAME,
				AdeWebContract.Schedule.SCHEDULE_CREATED,
				AdeWebContract.Schedule.SCHEDULE_LAST_UPDATE };

		final int _ID = 0;
		final int SCHEDULE_NAME = 1;
		final int SCHEDULE_DESCRIPTION = 2;
		final int SCHEDULE_RESOURCES = 3;
		final int SCHEDULE_STUDENT_NUM = 4;
		final int SCHEDULE_PERIOD = 5;
		final int SCHEDULE_PROJECT_NAME = 6;
		final int SCHEDULE_CREATED = 7;
		final int SCHEDULE_LAST_UPDATE = 8;
	}

}// class ScheduleDetailsFragment